package main.userInterface.islandui;


import javax.swing.*;
import javax.swing.event.*;

import main.model.island.IslandInterface;

import java.awt.*;
import java.awt.event.*;


@SuppressWarnings("serial")
public class IslandFrame extends JPanel{
    IslandCanvas islandCanvas;
    
    private int halfzoomMIN = 3;
    private int halfzoomMAX = 28;
    private int halfzoomINIT = 10;
    private boolean crits = false;
    
    private JScrollPane map;
    private JSlider zoomSlider;
    //private JCheckBox gridLineBox;
    private JCheckBox gridRefBox;
    private JCheckBox roundingBox;
    private JCheckBox critNumBox;
    
    private Font labelFont = new Font("Serif", Font.PLAIN, 12);

    public IslandFrame(IslandInterface island){
        islandCanvas = new IslandCanvas(island);
        formIslandFrame();
    }
  
    public void formIslandFrame(){
        this.setLayout(new BorderLayout());
        
        
        JPanel visPan = new JPanel(new FlowLayout(FlowLayout.CENTER));
        
        //Slider Settings
        JLabel zoomLab = new JLabel("Zoom: "); zoomLab.setFont(labelFont);
        JLabel inLab = new JLabel("-");			inLab.setFont(labelFont);
        JLabel outLab = new JLabel("+");		outLab.setFont(labelFont);
        UIManager.put("Slider.paintValue", false);
        zoomSlider = new JSlider(halfzoomMIN,halfzoomMAX,halfzoomINIT);
        setZoom();
        //islandCanvas.revalidate();//Resize the scrollpane
        visPan.add(zoomLab);
        visPan.add(inLab);
        visPan.add(zoomSlider);
        visPan.add(outLab);
        
        //Checkbox Settings
        //gridLineBox = new JCheckBox();
        //gridLineBox.setSelected(true);
        //JLabel glLab = new JLabel("Gridlines"); glLab.setFont(labelFont);
        gridRefBox = new JCheckBox();
        gridRefBox.setSelected(true);
        JLabel grLab = new JLabel("Grid #s"); grLab.setFont(labelFont);
        roundingBox = new JCheckBox();
        roundingBox.setSelected(true);
        JLabel roLab = new JLabel("Hi-res"); roLab.setFont(labelFont);
        critNumBox = new JCheckBox();
        critNumBox.setSelected(true);
        JLabel cnLab = new JLabel("C #s"); cnLab.setFont(labelFont);
        
        islandCanvas.setGridRef(gridRefBox.isSelected());
        //islandCanvas.setGridLine(gridLineBox.isSelected());
        islandCanvas.setRounded(roundingBox.isSelected());
       // islandCanvas.setCritNum(critNumBox.isSelected());
        
        //visPan settings
        //visPan.add(gridLineBox);
        //visPan.add(glLab);
        visPan.add(gridRefBox);
        visPan.add(grLab);
        visPan.add(roundingBox);
        visPan.add(roLab);
        if(crits){
            visPan.add(critNumBox);
            visPan.add(cnLab);
        }
        visPan.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Visual Settings"), BorderFactory.createEmptyBorder(0,0,0,0)), visPan.getBorder()));
        
        map = new JScrollPane(islandCanvas);
        map.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        map.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        map.setWheelScrollingEnabled(true);
        map.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Critter HexIsland"), BorderFactory.createEmptyBorder(0,0,0,0)), map.getBorder()));
        
        add(visPan,BorderLayout.NORTH);
        add(map,BorderLayout.CENTER);
        
        zoomSlider.addChangeListener(new ChangeListener(){
            public void stateChanged(ChangeEvent e){
            	setZoom();
                islandCanvas.revalidate();//Resize the scrollpane
            }
        });
        
        gridRefBox.addActionListener(new ActionListener(){ 
            public void actionPerformed(ActionEvent ae){
                islandCanvas.setGridRef(gridRefBox.isSelected());
            }
        });
        
        //gridLineBox.addActionListener(new ActionListener(){
        //    public void actionPerformed(ActionEvent ae){
        //        islandCanvas.setGridLine(gridLineBox.isSelected());
        //    }
        //});
        
        roundingBox.addActionListener(new ActionListener(){ 
            public void actionPerformed(ActionEvent ae){
                islandCanvas.setRounded(roundingBox.isSelected());
            }
        });
        
        
        /*critNumBox.addActionListener(new ActionListener(){ 
            public void actionPerformed(ActionEvent ae){
                islandCanvas.setCritNum(critNumBox.isSelected());
            }
        });*/
    }
    public void repaintIsland(){
        islandCanvas.repaint();
        islandCanvas.revalidate();
    }
    public void setIsland(IslandInterface island){
        islandCanvas.setIsland(island);
        islandCanvas.revalidate(); //Resize the scrollpane
    }
    /*public void setCRI(Critters CRIin){
        islandCanvas.setCRI(CRIin);
    }*/
    
    public void setGridRef(boolean setG){islandCanvas.setGridRef(setG); gridRefBox.setSelected(setG);}
    //public void setGridLine(boolean setG){islandCanvas.setGridLine(setG); gridLineBox.setSelected(setG);}

    private void setZoom() {
        islandCanvas.setZoom(zoomSlider.getValue()*2);
	}
    
	public IslandCanvas getIslandCanvas() {return islandCanvas;}
	public IslandInterface getIsland() {return islandCanvas.getIsland();}
}
